<template>
  <div>
    <home-header></home-header>
    <home-swiper :list = "swiperList"></home-swiper>
    <homeicons :list = "iconsList"></homeicons>
    <home-temai></home-temai>
    <home-like :list = "likeList"></home-like>
    <home-where :list = "whereList"></home-where>
  </div>
</template>

<script>
import HomeHeader from '@/pages/home/components/header'
import HomeSwiper from '@/pages/home/components/swiper'
import Homeicons from '@/pages/home/components/icons'
import HomeTemai from '@/pages/home/components/temai'
import HomeLike from '@/pages/home/components/like'
import HomeWhere from '@/pages/home/components/where'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  name: 'home',
  data () {
    return {
      swiperList: [],
      iconsList: [],
      likeList: [],
      whereList: [],
      lastCity: ''
    }
  },
  components: {
    HomeHeader,
    HomeSwiper,
    Homeicons,
    HomeTemai,
    HomeLike,
    HomeWhere
  },
  computed: {
    ...mapState(['city'])
  },
  mounted () {
    this.getAjaxInfo()
    this.lastCity = this.city
  },
  // keep-alive 组件激活时调用activated钩子函数
  activated () {
    if (this.lastCity !== this.city) {
      this.getAjaxInfo()
      this.lastCity = this.city
    }
  },
  methods: {
    getAjaxInfo () {
      axios.get('api/index.json?city=' + this.city).then(this.getInfoSuccess)
      // 这里的“/api”实现路径是“/static/ajax-data”，是通过webpack设置的
    },
    getInfoSuccess (res) {
      if (res.data.ret === true && res.data.data) {
        const info = res.data.data
        this.swiperList = info.swiperList
        this.iconsList = info.iconsList
        this.likeList = info.likeList
        this.whereList = info.whereList
      }
    }
  }
}
</script>

<style scoped>
</style>
